<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="申请人编号" prop="applicantId">
        <el-input
          v-model="queryParams.applicantId"
          placeholder="请输入申请人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验室编号" prop="labId">
        <el-input
          v-model="queryParams.labId"
          placeholder="请输入申请实验室编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请学期" prop="appliedSemester">
        <el-input
          v-model="queryParams.appliedSemester"
          placeholder="请输入申请学期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请周次" prop="appliedWeek">
        <el-input
          v-model="queryParams.appliedWeek"
          placeholder="请输入申请周次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星期几" prop="appliedDay">
        <el-select v-model="queryParams.appliedDay" placeholder="请选择星期几" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_day"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请节次" prop="appliedSession">
        <el-select v-model="queryParams.appliedSession" placeholder="请选择申请节次" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_session"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="填报日期" prop="fillDate">
        <el-date-picker clearable
          v-model="queryParams.fillDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择填报日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审批员编号" prop="approverId">
        <el-input
          v-model="queryParams.approverId"
          placeholder="请输入审批员编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="applicationStatus">
        <el-select v-model="queryParams.applicationStatus" placeholder="请选择申请状态" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_application_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jwxt:stu_application:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jwxt:stu_application:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jwxt:stu_application:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jwxt:stu_application:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stu_applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请编号" align="center" prop="applicationId" />
      <el-table-column label="申请人编号" align="center" prop="applicantId" />
      <el-table-column label="实验室编号" align="center" prop="labId" />
      <el-table-column label="申请原因" align="center" prop="applicationReason">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="申请原因"
            width="400"
            trigger="hover"
            :content="scope.row.applicationReason">
            <el-input
              slot="reference"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              readonly
              v-if="scope.row.applicationReason!=null"
              v-model="scope.row.applicationReason">
            </el-input>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="申请学期" align="center" prop="appliedSemester" />
      <el-table-column label="申请周次" align="center" prop="appliedWeek" />
      <el-table-column label="星期几" align="center" prop="appliedDay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jwxt_day" :value="scope.row.appliedDay"/>
        </template>
      </el-table-column>
      <el-table-column label="申请节次" align="center" prop="appliedSession">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jwxt_session" :value="scope.row.appliedSession"/>
        </template>
      </el-table-column>
      <el-table-column label="填报日期" align="center" prop="fillDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fillDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批员编号" align="center" prop="approverId" />
      <el-table-column label="申请状态" align="center" prop="applicationStatus">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="info"
            v-if="scope.row.applicationStatus=='未审核'"
          >{{ scope.row.applicationStatus }}</el-button>
          <el-button
            size="mini"
            type="primary"
            v-if="scope.row.applicationStatus=='通过'"
          >{{ scope.row.applicationStatus }}</el-button>
          <el-button
            size="mini"
            type="danger"
            v-if="scope.row.applicationStatus=='驳回'"
          >{{ scope.row.applicationStatus }}</el-button>
          <el-button
            size="mini"
            type="success"
            v-if="scope.row.applicationStatus=='使用完毕'"
          >完毕</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.applicationStatus=='未审核'"
            v-hasPermi="['jwxt:stu_application:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jwxt:stu_application:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sunrise"
            @click="handleFinish(scope.row)"
            v-if="scope.row.applicationStatus=='通过'"
            v-hasPermi="['jwxt:stu_application:confirm']"
          >使用完毕</el-button>
          <el-dropdown
            v-hasPermi="['jwxt:stu_application:check']"
            v-if="scope.row.applicationStatus=='未审核'">
            <el-button type="text" size="mini" icon="el-icon-arrow-down" >审核</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-check"
                  @click="handlePass(scope.row)"
                  v-hasPermi="['jwxt:stu_application:check']"
                >通过</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-close"
                  @click="handleReject(scope.row,false)"
                  v-hasPermi="['jwxt:stu_application:check']"
                >驳回</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生实验课申请登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="申请人编号" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人编号" />
        </el-form-item>
        <el-form-item label="申请原因" prop="applicationReason">
          <el-input v-model="form.applicationReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请学期" prop="appliedSemester">
          <el-tooltip class="item" effect="light" content="默认为当前学期" placement="top">
            <el-input v-model="form.appliedSemester" disabled />
          </el-tooltip>
        </el-form-item>
        <el-form-item label="申请周次" prop="appliedWeek">
          <el-slider
            v-model="form.appliedWeek"
            :min="1"
            :max="20"
          ></el-slider>
        </el-form-item>
        <el-form-item label="星期几" prop="appliedDay">
          <el-select v-model="form.appliedDay" placeholder="请选择星期几">
            <el-option
              v-for="dict in dict.type.jwxt_day"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请节次" prop="appliedSession">
          <el-select v-model="form.appliedSession" placeholder="请选择申请节次">
            <el-option
              v-for="dict in dict.type.jwxt_session"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="实验室编号" prop="labId">
            <el-input v-model="form.labId" disabled/>
            <el-tooltip class="item" effect="light" content="请先填写好上述信息" placement="top-start">
              <el-button type="text" icon="el-icon-refresh-right" @click="handleGetFreeLab" size="small">获取空余教室</el-button>
            </el-tooltip>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    空闲实验室对话框-->
    <el-dialog :title="title" :visible.sync="freeLabDialog" width="350px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="空闲实验室" prop="labId">
          <el-select v-model="form.labId" filterable placeholder="请选择空闲实验室">
            <el-option
              v-for="item in this.freelabOption"
              :key="item.labId"
              :label="`${item.labId} - ${item.labName}${item.labNumber}`"
              :value="item.labId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="()=>{
          this.freeLabDialog=false;
        }">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listStu_application,
  getStu_application,
  delStu_application,
  addStu_application,
  updateStu_application,
  pass_application,
  reject_application, confirm_application
} from "@/api/jwxt/stu_application";
import {get_validate_labs} from "@/api/jwxt/tea_application";

export default {
  name: "Stu_application",
  dicts: ['jwxt_day', 'jwxt_session', 'jwxt_application_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生实验课申请登记表格数据
      stu_applicationList: [],
      // 空闲实验室动态下拉框数据
      freelabOption:{},
      // 当前学期
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      freeLabDialog:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicantId: null,
        labId: null,
        applicationReason: null,
        appliedSemester: null,
        appliedWeek: null,
        appliedDay: null,
        appliedSession: null,
        fillDate: null,
        approverId: null,
        applicationStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applicantId: [{ required: true, message: '该项为必填项', trigger: 'blur' }],
        appliedSession: [{ required: true, message: '该项为必填项', trigger: 'blur' }],
        appliedWeek: [{ required: true, message: '该项为必填项', trigger: 'blur' }],
        appliedDay: [{ required: true, message: '该项为必填项', trigger: 'blur' }],
        labId:[{ required: true, message: '该项为必填项', trigger: 'blur' }],
        appliedSemester: [{ required: true, message: '该项为必填项', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生实验课申请登记列表 */
    getList() {
      this.loading = true;
      listStu_application(this.queryParams).then(response => {
        this.stu_applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applicationId: null,
        applicantId: null,
        labId: null,
        applicationReason: null,
        appliedSemester: null,
        appliedWeek: null,
        appliedDay: null,
        appliedSession: null,
        fillDate: null,
        approverId: null,
        applicationStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applicationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生实验课申请登记";
      this.form.applicantId=this.$store.state.user.id;
      this.loading=true;
      this.getConfigKey("jwxt.curSemester").then(response => {
        this.form.appliedSemester = response.msg;
        this.loading=false;
      }).catch(e=>{
        this.loading=false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applicationId = row.applicationId || this.ids
      getStu_application(applicationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生实验课申请登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applicationId != null) {
            updateStu_application(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.freeLabDialog=false;
              this.getList();
            });
          } else {
            addStu_application(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.freeLabDialog=false;
              this.getList();
            }).catch(e=>{
              this.$modal.msgError('该实验室无法使用！');
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applicationIds = row.applicationId || this.ids;
      this.$modal.confirm('是否确认删除学生实验课申请登记编号为"' + applicationIds + '"的数据项？').then(function() {
        return delStu_application(applicationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jwxt/stu_application/export', {
        ...this.queryParams
      }, `stu_application_${new Date().getTime()}.xlsx`)
    },
    /** 通过按钮操作 */
    handlePass(row) {
      this.reset();
      row.approverId=this.$store.state.user.id;
      pass_application(row).then(r=>{
        this.$message.success("已通过");
        this.getList();
      }).catch(e=>{
        this.$message.error("数据异常，无可用实验室！");
      });
    },
    /** 驳回按钮操作 */
    handleReject(row) {
      this.reset();
      row.approverId=this.$store.state.user.id;
      reject_application(row).then(r=>{
        this.$message.warning("已驳回");
        this.getList();
      });
    },
    /** 使用完毕按钮操作 */
    handleFinish(row){
      this.reset();
      confirm_application(row).then(r=>{
        this.$message.warning("已使用完成");
        this.getList();
      });
    },
    //获取空闲实验室按钮操作
    handleGetFreeLab(){
/*      this.$refs.form.validate(valid => {
        if(valid){
          this.freeLabDialog=true;
          this.fillFreeLabOption();
        }else{
          this.$message.error("申请信息不完整！");
        }
      })*/
      if(this.form.appliedWeek!=null && this.form.appliedDay!=null
        && this.form.appliedSession!=null){
        this.freeLabDialog=true;
        this.fillFreeLabOption();
      }else {
        this.$message.error("申请信息不完整！");
      }
    },
    // 填充空闲实验室动态下拉框数据
    fillFreeLabOption(){
      this.form.startWeek=this.form.appliedWeek;
      this.form.endWeek=this.form.appliedWeek;
      get_validate_labs(this.form).then(rep=>{
        this.freelabOption=rep.data;
        console.log(this.freelabOption);
      })
    }
  }
};
</script>
